package mybanlance;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 自动识别{} 匹配
 */

class KuohaoItem{
    private String name;
    private int lineno;
    private int pos;

    public KuohaoItem(String name, int lineno) {
        this.name = name;
        this.lineno = lineno;
    }

    public KuohaoItem(String name, int lineno, int pos) {
        this.name = name;
        this.lineno = lineno;
        this.pos = pos;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getLineno() {
        return lineno;
    }

    public void setLineno(int lineno) {
        this.lineno = lineno;
    }

    public int getPos() {
        return pos;
    }

    public void setPos(int pos) {
        this.pos = pos;
    }
}

public class MyBlanceDemo {
    private static List<KuohaoItem> lists = new ArrayList<>();
    public static boolean readFileWrapper(String fileName) throws Exception {
        BufferedReader br = new BufferedReader(new FileReader(new File(fileName)));

        String line = null ;
        int lineno = 1;
        while (null != (line=br.readLine())) {
            int leftkuohaoPos = line.indexOf('{');
            int rightkuohaoPos = line.indexOf('}');
            KuohaoItem kuohaoItemLeft = null;
            KuohaoItem kuohaoItemRight= null;

            if(leftkuohaoPos >=0){
                kuohaoItemLeft = new KuohaoItem("{", lineno, leftkuohaoPos);
            }

            if(rightkuohaoPos >= 0){
                kuohaoItemRight = new KuohaoItem("}", lineno, rightkuohaoPos);
            }

            if(kuohaoItemLeft != null && null == kuohaoItemRight){
                lists.add(new KuohaoItem("{", lineno));
            }
            else if (kuohaoItemLeft != null && null != kuohaoItemRight){
                if (kuohaoItemLeft.getPos() < kuohaoItemRight.getPos()){
                    if(lists.size() > 0){
                        lists.remove(lists.size()-1);
                    }
                    else {
                        System.err.println("出错......");
                        System.exit(-1);
                    }
                }
                else{
                    // do nothing
                }
            }
            else if (kuohaoItemLeft == null && null != kuohaoItemRight){
                if(lists.size() > 0){
                    lists.remove(lists.size()-1);
                }
                else {
                    System.err.println("出错......");
                    System.exit(-1);
                }
            }
            else {
                // null null 忽略
            }

            lineno ++;
        }
        br.close();

        if(lists.size() > 0){
            System.out.println("err:");
            return false;
        }

        return true;
    }


    public static void main(String[] args) throws Exception {
        System.out.println(MyBlanceDemo.readFileWrapper("C:\\MyNameNode.java"));

    }

}
